package com.sky.loveshop.controller;

import cn.hutool.json.JSONUtil;
import com.sky.loveshop.entity.Result;
import com.sky.loveshop.entity.common.PageResult;
import com.sky.loveshop.entity.exception.BizException;
import com.sky.loveshop.entity.exception.BizExceptionEnum;
import com.sky.loveshop.entity.qo.OrderInfoQO;
import com.sky.loveshop.entity.qo.PrivateOrderInfoQO;
import com.sky.loveshop.entity.vo.OrderInfoVO;
import com.sky.loveshop.entity.vo.PrivateOrderInfoVO;
import com.sky.loveshop.service.OrderInfoService;
import com.sky.loveshop.service.PrivateOrderInfoService;
import com.sky.loveshop.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author xcy
 * @version V1.0
 * @description
 * @date 2024/9/1 2:13
 */
@Slf4j
@RestController
@RequestMapping("/orderInfo")
public class OrderInfoController {

    @Autowired
    private OrderInfoService orderInfoService;
    @Autowired
    private PrivateOrderInfoService privateOrderInfoService;

    /**
     * 线下门店订单分页查询
     */
    @PostMapping("/listOrderInfoPages")
    public Result<PageResult<OrderInfoVO>> listOrderInfoPages(@RequestBody OrderInfoQO orderInfoQO) {
        return Result.page(orderInfoService.listOrderInfoPages(orderInfoQO));
    }

    /**
     * 线下门店订单根据id获取详情
     */
    @GetMapping("/getOrderInfoById")
    public Result<OrderInfoVO> getOrderInfoById(Integer id) throws BizException {
        if (id == null) {
            throw new BizException(BizExceptionEnum.ID_MUST_NEED);
        }
        return Result.success(orderInfoService.getOrderInfoById(id));
    }

    /**
     * 私域用户下单
     */
    @PostMapping("/addPrivateOrder")
    public Result<Integer> addPrivateOrder(@RequestBody PrivateOrderInfoQO qo) throws BizException {
        log.info("addPrivateOrderParam={}", JSONUtil.toJsonStr(qo));
        if (StringUtils.isEmpty(qo.getReceiverName()) || StringUtils.isEmpty(qo.getReceiverPhone()) ||
                StringUtils.isEmpty(qo.getReceiverAddress()) || null == qo.getReceiverLat() || null == qo.getReceiverLng()) {
            throw new BizException(BizExceptionEnum.PARAMETER_ERROR);
        }
        if (!StringUtils.isValidPhoneNumber(qo.getReceiverPhone())) {
            throw new BizException(BizExceptionEnum.PHONE_NUMBER_ERROR);
        }
        privateOrderInfoService.addPrivateOrder(qo);
        return Result.success();
    }

    /**
     * 私域订单列表分页查询
     */
    @PostMapping("/listPrivateOrderInfoPages")
    public Result<PageResult<PrivateOrderInfoVO>> listPrivateOrderInfoPages(@RequestBody PrivateOrderInfoQO qo) {
        log.info("listPrivateOrderInfoPagesParam={}", JSONUtil.toJsonStr(qo));
        return Result.page(privateOrderInfoService.listPrivateOrderInfoPages(qo));
    }

    /**
     * 私域订单根据id获取详情
     */
    @GetMapping("/getPrivateOrderInfoById")
    public Result<PrivateOrderInfoVO> getPrivateOrderInfoById(Integer id) throws BizException {
        if (id == null) {
            throw new BizException(BizExceptionEnum.ID_MUST_NEED);
        }
        return Result.success(privateOrderInfoService.getPrivateOrderInfoById(id));
    }

}
